package com.hk.dao.impl;

import com.hk.dao.IUserDao;
import com.hk.model.User;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.ResultSet;

/**
 * @Author: zhe
 * @Date: 2021/6/8 15:09
 * @Statement: 会员数据层
 */
public class UserDaoImpl implements IUserDao {

    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();

    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit, User t) {
        sql = "select * from user where 1=1 ";
        if (!t.getUsername().equals("")) {
            sql += " and username like "+SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getUsername()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (!t.getBirthday().equals("")) {
            sql += " and birthday = "+SqlSymbol.MARK.getValue()+t.getBirthday()+SqlSymbol.MARK.getValue();
        }
        if (!t.getGmtCreate().equals("")) {
            sql += " and gmt_create between "+
                    SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+
                    " and "+
                    SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue();
        }
        if (!t.getHobby().equals("")) {
            sql += "and (";
            String[] hobbys = t.getHobby().split(",");
            for (int i = 0; i < hobbys.length; i++) {
                sql += i==0?" find_in_set('" + hobbys[i] + "',hobby)":" or find_in_set('" + hobbys[i] + "',hobby)";
            }
            sql += ")";
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, User t) {
        sql = "select count(1) from user where 1=1 ";
        if (!t.getUsername().equals("")) {
            sql += " and username like "+SqlSymbol.MARK.getValue()+SqlSymbol.PERCENT.getValue()+t.getUsername()+SqlSymbol.PERCENT.getValue()+SqlSymbol.MARK.getValue();
        }
        if (!t.getBirthday().equals("")) {
            sql += " and birthday = "+SqlSymbol.MARK.getValue()+t.getBirthday()+SqlSymbol.MARK.getValue();
        }
        if (!t.getGmtCreate().equals("")) {
            sql += " and gmt_create between "+
                    SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+
                    " and "+
                    SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue();
        }
        if (!t.getHobby().equals("")) {
            sql += "and (";
            String[] hobbys = t.getHobby().split(",");
            for (int i = 0; i < hobbys.length; i++) {
                sql += i==0?" find_in_set('" + hobbys[i] + "',hobby)":" or find_in_set('" + hobbys[i] + "',hobby)";
            }
            sql += ")";
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from user where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(User t) {
        sql = "insert into user value " +
                "("+null+ SqlSymbol.COMMA.getValue()+SqlSymbol.MARK.getValue()+t.getUsername()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getEmail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getPassword()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getSex()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getBirthday()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getHobby()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getPhone()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getSite()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getState()+SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(User t) {
        sql = "update user set " +
                "username = "+SqlSymbol.MARK.getValue()+t.getUsername()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "email = "+SqlSymbol.MARK.getValue()+t.getEmail()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "sex = "+t.getSex()+SqlSymbol.COMMA.getValue()+
                "birthday = "+SqlSymbol.MARK.getValue()+t.getBirthday()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "hobby = "+SqlSymbol.MARK.getValue()+t.getHobby()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "phone = "+SqlSymbol.MARK.getValue()+t.getPhone()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "site = "+SqlSymbol.MARK.getValue()+t.getSite()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int updatePassWord(User t) {
        sql = "update user set " +
                "password = "+SqlSymbol.MARK.getValue()+t.getPassword()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int updateState(User t) {
        sql = "update user set " +
                "state = "+t.getState()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int delete(Integer id) {
        sql = "delete from user where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    /**
     * 通过邮箱查询数据库是否存在此邮箱
     * @param email
     * @return
     */
    @Override
    public ResultSet findByEmail(String email) {
        sql = "select * from user where email = "+SqlSymbol.MARK.getValue()+email+SqlSymbol.MARK.getValue();
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

}
